package io.fileWrite;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class fileWrite {
    public static void main(String[] args) {
        fileWrite fileWrite = new fileWrite();
        fileWrite.copy();
    }
    public void create(){
        int readLean = 0;
        String path = "d:/Test/create1.txt";
        FileReader fileReader = null;
        try {
            char[] chars = new char[1024];
            fileReader = new FileReader(path);
            while ((readLean=fileReader.read(chars))!=-1){
                System.out.println(new String(chars,0,readLean));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileReader != null) fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void copy(){
        int readLean = 0;
        String source = "d:/Test/create1.txt";
        String target = "d:/Test/create3.txt";
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            char[] chars = new char[1024];
            fileReader = new FileReader(source);
            fileWriter = new FileWriter(target,true);
            while ((readLean=fileReader.read(chars))!=-1){
                fileWriter.write(chars,0,readLean);
                fileWriter.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileReader != null) fileReader.close();
                if (fileWriter != null) fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
